package com.lry.util.captcha;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;
import javax.imageio.ImageIO;

/**
 * 生成验证码的工具类
 *
 * @author 李瑞益
 * @date 2019/9/3
 */
public class CaptchaBuilder {

    // 生成验证码图片的宽度
    private static final int width = 100;
    // 生成验证码图片的高度
    private static final int height = 50;
    private static final String[] fontNames = {"宋体", "楷体", "隶书", "微软雅黑"};
    // 定义验证码图片的背景颜色为白色
    private static final Color bgColor = new Color(255, 255, 255);
    private static final Random random = new Random();
    private static String codes = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
    // 记录随机字符串
    private String text;

    /**
     * 获取一个随意颜色
     *
     * @return 颜色
     */
    private static Color randomColor() {
        int red = random.nextInt(150);
        int green = random.nextInt(150);
        int blue = random.nextInt(150);
        return new Color(red, green, blue);
    }

    /**
     * 获取一个随机字体
     *
     * @return 字体
     */
    private static Font randomFont() {
        String name = fontNames[random.nextInt(fontNames.length)];
        int style = random.nextInt(4);
        int size = random.nextInt(5) + 24;
        return new Font(name, style, size);
    }

    /**
     * 获取一个随机字符
     *
     * @return 字符
     */
    private static char randomChar() {
        return codes.charAt(random.nextInt(codes.length()));
    }

    /**
     * 创建一个空白的BufferedImage对象
     *
     * @return 图片对象
     */
    private static BufferedImage createImage() {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        g2.setColor(bgColor);
        g2.fillRect(0, 0, width, height);
        return image;
    }

    /**
     * 获取验证码图片
     * @return 图片
     */
    public static Captcha buildCaptcha() {
        BufferedImage image = createImage();
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            String s = randomChar() + "";
            sb.append(s);
            g2.setColor(randomColor());
            g2.setFont(randomFont());
            float x = i * width * 1.0f / 4;
            g2.drawString(s, x, height - 15);
        }
        drawLine(image);
        Captcha captcha = new Captcha();
        captcha.setText(sb.toString());
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "jpg", byteArrayOutputStream);
        } catch (IOException e) {
            return null;
        }
        captcha.setImg(byteArrayOutputStream.toByteArray());
        captcha.setCaptchasId(UUID.randomUUID().toString());
        return captcha;
    }

    /**
     * 绘制干扰线
     *
     * @param image 验证码图片
     */
    private static void drawLine(BufferedImage image) {
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        int num = 5;
        for (int i = 0; i < num; i++) {
            int x1 = random.nextInt(width);
            int y1 = random.nextInt(height);
            int x2 = random.nextInt(width);
            int y2 = random.nextInt(height);
            g2.setColor(randomColor());
            g2.setStroke(new BasicStroke(1.5f));
            g2.drawLine(x1, y1, x2, y2);
        }
    }

}